﻿(function () {
    'user strict'
    appModule.factory("sdmap", ['$window', '$q', function ($window, $q) {
        return function (mapId, config) {
            //debugger;
            var _this = this;

            //需要显示地图的元素
            this.mapId = mapId;

            //服务配置
            this.config = config;

            //地图对象
            this.map = null;

            //加载百度地图api方法
            this.load = function () {

                if (!$window.init) {

                    var script = document.createElement("script");

                    script.type = "text/javascript";

                    //url最后的callback=init,表示加载完成后调用window对象上的初始化方法。
                    script.src = "http://www.sdmap.gov.cn/api/agsapi/javascript/js/init.js&callback=init";

                    document.body.appendChild(script);

                } else {

                    $window.init(this.mapId, this.config);

                };
                //定义初始化方法，挂到window对象上
                $window.init = function (mapId, config) {

                    //引入天地图山东矢量图层 
                    require(["esri/map", "tdlib/SDTDTLayer", "esri/geometry/Extent", "dojo/domReady!"],
                        function (Map, SDTDTLayer, Extent) {
                            //地图初始化 
                            _this.map = new Map(mapId,
                                {
                                    logo: false,
                                    slider: false,
                                    nav: false,
                                    extent: new Extent({ xmin: 116.5, ymin: 36.2, xmax: 117.5, ymax: 37.2, spatialReference: { wkid: 4490 } })
                                });
                            //图层初始化 
                            var sdvecLayer = new SDTDTLayer();
                            //添加图层到地图 
                            _this.map.addLayer(sdvecLayer);

                        }
                    ); 

                };
            };
        }

    }]);

})();